package dao

import (
	"context"
	"mall/api/mall"
	"mall/internal/model"
)

func (d *Dao) FindMemberStatsOrdersByPage(ctx context.Context, sgId, pStoreId, pageNo, pageSize uint32,
	memberIds []int64) (orders []*model.MemberOrderWebQuery, total int64, err error) {
	db := d.TransContext(ctx).Model(&model.Order{}).Select(
		"submit_time, order_number, trans, refunded").Where(
		"member_id in ? AND sg_id = ? AND p_store_id = ? AND status = ?",
		memberIds, sgId, pStoreId, uint8(mall.MallOrderStatus_MALL_PAYED))
	if err = db.Count(&total).Error; err != nil {
		err = d.processSqlError(err)
		return
	}
	err = d.processSqlError(db.Offset(int((pageNo - 1) * pageSize)).Limit(int(pageSize)).Order(
		"id desc").Scan(&orders).Error)
	return
}
